package com.alibaba.moco.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class BaseTypeUtil {

	/*static   String = "Backslash";*/
	private static final String BACKSLASH = "\\";
	private static final String SLASH = "/";
	private static final String DOUBLESLASH = "//";
	/**
	 * 将url处理为统一格式
	 * 处理为/xxxx/xxxx
	 * @param url
	 * @return
	 */
	public static String beautifulUrl(String url){
		if (!StringUtils.isBlank(url)){
			url = url.replace(BACKSLASH, SLASH);
			while (url.contains(DOUBLESLASH)){
				url = url.replace(DOUBLESLASH, SLASH);
			}
			if (!url.startsWith(SLASH)){
				url = new StringBuilder().append(SLASH).append(url).toString();
			}
			if (url.endsWith(SLASH)){
				url = new StringBuilder(url).deleteCharAt(url.length()-1).toString();
			}
			return url;
		}
		return null;
	}
	/**
	 * 获取应用根路径
	 * @return
	 */
	public static String getClassPath() {
		return Thread.currentThread().getContextClassLoader()
				.getResource("").getPath();
	}


	/**
	 * 將RequestMap轉換為Bean
	 * @param map
	 * @param clasz
	 * @return
	 */
	public static Object mapToBean(Map<String,String[]> map , Class<?> clasz ){
		try {
			Object object = clasz.newInstance();
			Method[] methods = clasz.getDeclaredMethods();
			for (Method method:methods){
				String methodName = method.getName();
				if (methodName!=null&&methodName.startsWith("set")){
					Class<?>[] params = method.getParameterTypes();
					if (params!= null && params.length == 1){
						char[] methodNames = methodName.toCharArray();
						if (methodNames.length<4) return object;
						String feildName = new StringBuilder().append(
								String.valueOf(methodNames[3]).toLowerCase()).append(methodName.substring(4)).toString();
						
						String[]   value = map.containsKey(feildName)?map.get(feildName):null;
						//获取参数值类型
						if (null != value && value.length>0){
							if (null!= value && params[0].equals(String.class)){
								method.invoke(object, new Object[]{value[0]});
							} else if (null!= value && params[0].equals(int.class) && !StringUtils.isBlank(value[0])){
								method.invoke(object, new Object[]{Integer.valueOf(value[0])});
							} else if (null!= value && params[0].equals(double.class) && !StringUtils.isBlank(value[0])){
								method.invoke(object, new Object[]{Double.valueOf(value[0])});
							} else if (null!= value && params[0].equals(Double.class) && !StringUtils.isBlank(value[0])){
								method.invoke(object, new Object[]{Double.valueOf(value[0])});
							} else if (null!= value && params[0].equals(Integer.class) && !StringUtils.isBlank(value[0])){
								method.invoke(object, new Object[]{Integer.valueOf(value[0])});
							} else if (null!= value && params[0].equals(BigDecimal.class) && !StringUtils.isBlank(value[0])){
								method.invoke(object, new Object[]{new BigDecimal(value[0])});
							} 
						}
					}
				}
			}
			return object;
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 將JSON轉換為Bean
	 * @param map
	 * @param clasz
	 * @return
	 */
	public static Object jsonToBean(Map<String,Object> map , Class<?> clasz ){
		try {
			Object object = clasz.newInstance();
			Method[] methods = clasz.getDeclaredMethods();
			for (Method method:methods){
				String methodName = method.getName();
				if (methodName!=null&&methodName.startsWith("set")){
					Class<?>[] params = method.getParameterTypes();
					if (params!= null && params.length == 1){
						char[] methodNames = methodName.toCharArray();
						if (methodNames.length<4) return object;
						String feildName = new StringBuilder().append(
								String.valueOf(methodNames[3]).toLowerCase()).append(methodName.substring(4)).toString();
						
						Object value = map.containsKey(feildName)?map.get(feildName):null;
						
						//获取参数值类型
						if (null != value ){
							String val = String.valueOf(value);
							if (null!= value && params[0].equals(String.class)){
								method.invoke(object, new Object[]{val});
							} else if (null!= value && params[0].equals(int.class)){
								method.invoke(object, new Object[]{Integer.valueOf(val)});
							} else if (null!= value && params[0].equals(double.class)){
								method.invoke(object, new Object[]{Double.valueOf(val)});
							} else if (null!= value && params[0].equals(Double.class)){
								method.invoke(object, new Object[]{Double.valueOf(val)});
							} else if (null!= value && params[0].equals(Integer.class)){
								method.invoke(object, new Object[]{Integer.valueOf(val)});
							} else if (null!= value && params[0].equals(BigDecimal.class)){
								method.invoke(object, new Object[]{new BigDecimal(val)});
							}
						}
					}
				}
			}
			return object;
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取随机整数
	 * @param max 最大值
	 * @return
	 */
	public static String randomIntNum(int max){
		return String.valueOf(Math.abs(Math.random()*max));
	}
	/**
	 * 获取随机小数
	 * @param max 最大值
	 * @return
	 */
	public static String randomDoubleNum(double max){
		DecimalFormat format = new DecimalFormat("#.00");
		return String.valueOf(format.format(Math.abs(Math.random()*max)));
	}
	
	public static String currentDateString(){
		String paramFormat = "yyyy-MM-dd HH:mm:ss";
	    SimpleDateFormat sdf = new SimpleDateFormat(paramFormat);
	    return sdf.format(System.currentTimeMillis()).toString();
	}
}
